package com.moinut.studentattendancemanager.view.teacher;

import com.moinut.studentattendancemanager.UserManager;
import com.moinut.studentattendancemanager.controller.AttendanceController;
import com.moinut.studentattendancemanager.controller.callback.OnGetListListener;
import com.moinut.studentattendancemanager.controller.callback.OnUpdateListener;
import com.moinut.studentattendancemanager.model.vo.AttendanceInfo;
import com.moinut.studentattendancemanager.view.BaseFrame;
import com.moinut.studentattendancemanager.utils.WindowJumper;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

/**
 * @author MOILING
 */
public class ChangeAttendanceFrame extends BaseFrame<ChangeAttendanceFrame.ChangeAttendancePanel> {

    private static final int DEFAULT_WIDTH = 500;
    private static final int DEFAULT_HEIGHT = 500;

    public ChangeAttendanceFrame() {
        super("修改考勤信息", DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }

    @Override
    protected ChangeAttendanceFrame.ChangeAttendancePanel onSetContentPane() {
        return new ChangeAttendanceFrame.ChangeAttendancePanel();
    }

    /* package */ class ChangeAttendancePanel extends JPanel implements ActionListener {
        private JTable mResultTable;
        private JScrollPane mResultScrollPane;
        private JButton mBackButton, mOkButton;
        private JPanel mResultPanel, mButtonPanel;
        private DefaultTableModel mTableModel;
        private TableColumnModel mColumnModel;
        private boolean mIsSuccess = true;

        /*package*/ ChangeAttendancePanel() {
            setLayout(new BorderLayout());
            initResultPanel();
            initButtonPanel();
            AttendanceController.getAttendancesInfoByTeacherId(UserManager.userId, new OnGetListListener<AttendanceInfo>() {
                @Override
                public void onSuccess(List<AttendanceInfo> attendancesInfo) {
                    clearTable();
                    for (AttendanceInfo attendanceInfo : attendancesInfo) {
                        mTableModel.addRow(new Object[]{
                                attendanceInfo.getCourseId(),
                                attendanceInfo.getCourse(),
                                attendanceInfo.getStudentId(),
                                attendanceInfo.getStudent(),
                                attendanceInfo.getTime(),
                                attendanceInfo.getState()
                        });
                    }
                }

                @Override
                public void onError(String error) {
                    clearTable();
                    JOptionPane.showMessageDialog(null, error, "错误", JOptionPane.ERROR_MESSAGE);
                }
            });
        }

        private void clearTable() {
            for (int i = mTableModel.getRowCount() - 1; i >= 0; i--) {
                mTableModel.removeRow(i);
            }
        }

        private void initButtonPanel() {
            mOkButton = new JButton("确认修改");
            mBackButton = new JButton("返回上级菜单");
            mButtonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mButtonPanel.add(mOkButton);
            mButtonPanel.add(mBackButton);
            add(mButtonPanel, BorderLayout.SOUTH);
            mOkButton.addActionListener(this);
            mBackButton.addActionListener(this);
        }

        private void initResultPanel() {
            String[] columnName = {"教学班号", "课程名称", "学生ID", "姓名", "考勤时间", "考勤记录"};
            Object[][] data = {
                    {"1003", "面向对象程序设计", "1310001", "王二", "2016-06-16", "正常"},
                    {"1003", "面向对象程序设计", "1310001", "王二", "2016-06-16", "正常"},
                    {"1003", "面向对象程序设计", "1310001", "王二", "2016-06-16", "正常"},
                    {"1003", "面向对象程序设计", "1310001", "王二", "2016-06-16", "正常"},
                    {"1003", "面向对象程序设计", "1310001", "王二", "2016-06-16", "正常"}
            };
            mTableModel = new DefaultTableModel(data, columnName);
            mResultTable = new JTable(mTableModel);
            mColumnModel = mResultTable.getColumnModel();
            setEditableColumn();
            mResultScrollPane = new JScrollPane(mResultTable,
                    ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,
                    ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);

            mResultPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
            mResultPanel.add(mResultScrollPane);
            add(mResultPanel, BorderLayout.CENTER);
        }

        private void setEditableColumn() {
            JComboBox<String> attendanceStateCombo = new JComboBox<>();
            attendanceStateCombo.addItem("正常");
            attendanceStateCombo.addItem("迟到");
            attendanceStateCombo.addItem("旷课");
            attendanceStateCombo.addItem("请假");
            mColumnModel.getColumn(5).setCellEditor(new DefaultCellEditor(attendanceStateCombo));
            mResultTable.setEnabled(true);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            if (e.getSource().equals(mBackButton)) {
                WindowJumper.back(ChangeAttendanceFrame.this);
            } else if (e.getSource().equals(mOkButton)) {
                mIsSuccess = true;
                for (int i = 0; i < mTableModel.getRowCount(); i++) {
                    AttendanceInfo attendanceInfo = new AttendanceInfo((String) mTableModel.getValueAt(i, 0), (String) mTableModel.getValueAt(i, 1),
                            (String) mTableModel.getValueAt(i, 2), (String) mTableModel.getValueAt(i, 3),
                            (String) mTableModel.getValueAt(i, 4), (String) mTableModel.getValueAt(i, 5));
                    AttendanceController.updateAttendanceInfo(attendanceInfo, new OnUpdateListener() {
                        @Override
                        public void onSuccess(String success) {
                        }

                        @Override
                        public void onError(String error) {
                            System.out.println(error);
                            mIsSuccess = false;
                        }
                    });
                }
                if (mIsSuccess) {
                    JOptionPane.showMessageDialog(null, "修改成功", "成功", JOptionPane.INFORMATION_MESSAGE);
                } else {
                    JOptionPane.showMessageDialog(null, "修改出错", "错误", JOptionPane.ERROR_MESSAGE);
                }
            }
        }
    }

}
